import { ElLoading } from "element-plus";

interface LoadingState {
  count: number;
  // instance: LoadingInstance | null;
  // instance: ReturnType<typeof ElLoading.service> | null;
  instance: any | null;
}

export const useLoadingStore = defineStore("loading", {
  state: (): LoadingState => {
    return {
      count: 0,
      instance: null,
    };
  },
  getters: {},
  actions: {
    showLoading() {
      if (this.count++ === 0) {
        //初始化 instance
        this.instance = ElLoading.service();
      }
    },
    hideLoading() {
      if (this.count-- == 1) {
        this.instance?.close();
        this.instance = null;
      }
    },
  },
});
